<!DOCTYPE html>
<html lang=zh>
<head>
    <meta charset="utf-8">
    
    <title>Java运算符 | 紫焱</title>
    
    
        <meta name="keywords" content="语法规则" />
    
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="计算机的最基本用途之一就是执行数学运算， Java也提供了一套丰富的运算符来操纵变量。 算术运算符   操作符 描述      + 加法 - 相加运算符两侧的值    - 减法 - 左操作数减去右操作数    * 乘法 - 相乘操作符两侧的值    / 除法 - 左操作数除以右操作数    ％ 取余 - 左操作数除以右操作数的余数    ++ 自增: 操作数的值增加1    – 自减: 操作数的值">
<meta name="keywords" content="语法规则">
<meta property="og:type" content="article">
<meta property="og:title" content="Java运算符">
<meta property="og:url" content="http://yoursite.com/2018/08/12/Java运算符/index.html">
<meta property="og:site_name" content="紫焱">
<meta property="og:description" content="计算机的最基本用途之一就是执行数学运算， Java也提供了一套丰富的运算符来操纵变量。 算术运算符   操作符 描述      + 加法 - 相加运算符两侧的值    - 减法 - 左操作数减去右操作数    * 乘法 - 相乘操作符两侧的值    / 除法 - 左操作数除以右操作数    ％ 取余 - 左操作数除以右操作数的余数    ++ 自增: 操作数的值增加1    – 自减: 操作数的值">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-09-07T12:10:11.858Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java运算符">
<meta name="twitter:description" content="计算机的最基本用途之一就是执行数学运算， Java也提供了一套丰富的运算符来操纵变量。 算术运算符   操作符 描述      + 加法 - 相加运算符两侧的值    - 减法 - 左操作数减去右操作数    * 乘法 - 相乘操作符两侧的值    / 除法 - 左操作数除以右操作数    ％ 取余 - 左操作数除以右操作数的余数    ++ 自增: 操作数的值增加1    – 自减: 操作数的值">
    

    
        <link rel="alternate" href="/atom.xml" title="紫焱" type="application/atom+xml" />
    

    
        <link rel="icon" href="/favicon.ico" />
    

    <link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/libs/open-sans/styles.css">
    <link rel="stylesheet" href="/libs/source-code-pro/styles.css">

    <link rel="stylesheet" href="/css/style.css">
    <script src="/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="/libs/jquery/plugins/cookie/1.4.1/jquery.cookie.js"></script>
    
    
        <link rel="stylesheet" href="/libs/lightgallery/css/lightgallery.min.css">
    
    
        <link rel="stylesheet" href="/libs/justified-gallery/justifiedGallery.min.css">
    
    
    
    


    
        <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    
</head>

<body>
    <div id="container">
        <header id="header">
    <div id="header-main" class="header-inner">
        <div class="outer">
            <a href="/" id="logo">
                <i class="logo"></i>
                <span class="site-title">紫焱</span>
            </a>
            <nav id="main-nav">
                
                    <a class="main-nav-link" href="/">首页</a>
                
                    <a class="main-nav-link" href="/archives">归档</a>
                
                    <a class="main-nav-link" href="/categories">分类</a>
                
                    <a class="main-nav-link" href="/tags">标签</a>
                
                    <a class="main-nav-link" href="/about">关于</a>
                
            </nav>
            
            <div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="想要查找什么..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div>
        </div>
    </div>
    <div id="main-nav-mobile" class="header-sub header-inner">
        <table class="menu outer">
            <tr>
                
                    <td><a class="main-nav-link" href="/">首页</a></td>
                
                    <td><a class="main-nav-link" href="/archives">归档</a></td>
                
                    <td><a class="main-nav-link" href="/categories">分类</a></td>
                
                    <td><a class="main-nav-link" href="/tags">标签</a></td>
                
                    <td><a class="main-nav-link" href="/about">关于</a></td>
                
                <td>
                    
    <div class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
    </div>

                </td>
            </tr>
        </table>
    </div>
</header>

        <div class="outer">
            
            
                <aside id="sidebar">
   
        
    <div class="widget-wrap" id='categories'>
        <h3 class="widget-title">
            <span>分类</span>
            &nbsp;
            <a id='allExpand' href="#">
                <i class="fa fa-angle-double-down fa-2x"></i>
            </a>
        </h3>
        
        
        
         <ul class="unstyled" id="tree" > 
                    <li class="directory open">
                        <a href="#" data-role="directory">
                            <i class="fa fa-folder-open"></i>
                            &nbsp;
                            Java
                        </a>
                         <ul class="unstyled" id="tree" > 
                    <li class="directory open">
                        <a href="#" data-role="directory">
                            <i class="fa fa-folder-open"></i>
                            &nbsp;
                            基础语法
                        </a>
                         <ul class="unstyled" id="tree" >  <li class="file"><a href="/2018/08/12/Java安装/">Java环境安装</a></li>  <li class="file"><a href="/2018/08/12/Java数据类型/">Java数据类型</a></li>  <li class="file"><a href="/2018/08/12/Java修饰符/">Java修饰符</a></li>  <li class="file"><a href="/2018/08/12/Java命名注释/">Java命名与注释</a></li>  <li class="file active"><a href="/2018/08/12/Java运算符/">Java运算符</a></li>  <li class="file"><a href="/2018/09/07/Java语句/">Java语句</a></li>  </ul> 
                    </li> 
                     </ul> 
                    </li> 
                    
                    <li class="directory">
                        <a href="#" data-role="directory">
                            <i class="fa fa-folder"></i>
                            &nbsp;
                            git
                        </a>
                         <ul class="unstyled" id="tree" >  <li class="file"><a href="/2018/12/01/git操作命令/">git操作命令</a></li>  </ul> 
                    </li> 
                     <li class="file"><a href="/2018/08/11/index/">index</a></li>  </ul> 
    </div>
    <script>
        $(document).ready(function() {
            var iconFolderOpenClass  = 'fa-folder-open';
            var iconFolderCloseClass = 'fa-folder';
            var iconAllExpandClass = 'fa-angle-double-down';
            var iconAllPackClass = 'fa-angle-double-up';
            // Handle directory-tree expansion:
            // 左键单独展开目录
            $(document).on('click', '#categories a[data-role="directory"]', function (event) {
                event.preventDefault();

                var icon = $(this).children('.fa');
                var expanded = icon.hasClass(iconFolderOpenClass);
                var subtree = $(this).siblings('ul');
                icon.removeClass(iconFolderOpenClass).removeClass(iconFolderCloseClass);
                if (expanded) {
                    if (typeof subtree != 'undefined') {
                        subtree.slideUp({ duration: 100 });
                    }
                    icon.addClass(iconFolderCloseClass);
                } else {
                    if (typeof subtree != 'undefined') {
                        subtree.slideDown({ duration: 100 });
                    }
                    icon.addClass(iconFolderOpenClass);
                }
            });
            // 右键展开下属所有目录
            $('#categories a[data-role="directory"]').bind("contextmenu", function(event){
                event.preventDefault();
                
                var icon = $(this).children('.fa');
                var expanded = icon.hasClass(iconFolderOpenClass);
                var listNode = $(this).siblings('ul');
                var subtrees = $.merge(listNode.find('li ul'), listNode);
                var icons = $.merge(listNode.find('.fa'), icon);
                icons.removeClass(iconFolderOpenClass).removeClass(iconFolderCloseClass);
                if(expanded) {
                    subtrees.slideUp({ duration: 100 });
                    icons.addClass(iconFolderCloseClass);
                } else {
                    subtrees.slideDown({ duration: 100 });
                    icons.addClass(iconFolderOpenClass);
                }
            })
            // 展开关闭所有目录按钮
            $(document).on('click', '#allExpand', function (event) {
                event.preventDefault();
                
                var icon = $(this).children('.fa');
                var expanded = icon.hasClass(iconAllExpandClass);
                icon.removeClass(iconAllExpandClass).removeClass(iconAllPackClass);
                if(expanded) {
                    $('#sidebar .fa.fa-folder').removeClass('fa-folder').addClass('fa-folder-open')
                    $('#categories li ul').slideDown({ duration: 100 });
                    icon.addClass(iconAllPackClass);
                } else {
                    $('#sidebar .fa.fa-folder-open').removeClass('fa-folder-open').addClass('fa-folder')
                    $('#categories li ul').slideUp({ duration: 100 });
                    icon.addClass(iconAllExpandClass);
                }
            });  
        });
    </script>

    
    <div id="toTop" class="fa fa-angle-up"></div>
</aside>
            
            <section id="main"><article id="post-Java运算符" class="article article-type-post" itemscope itemprop="blogPost">
    <div class="article-inner">
        
        
            <header class="article-header">
                
                    <div class="article-meta">
                        
    <div class="article-category">
    	<i class="fa fa-folder"></i>
        <a class="article-category-link" href="/categories/Java/">Java</a><i class="fa fa-angle-right"></i><a class="article-category-link" href="/categories/Java/基础语法/">基础语法</a>
    </div>

                        
    <div class="article-tag">
        <i class="fa fa-tag"></i>
        <a class="tag-link" href="/tags/语法规则/">语法规则</a>
    </div>

                        
    <div class="article-date">
        <i class="fa fa-calendar"></i>
        <a href="/2018/08/12/Java运算符/">
            <time datetime="2018-08-12T01:30:19.000Z" itemprop="datePublished">2018-08-12</time>
        </a>
    </div>


                        
                            <i class="fa fa-bar-chart"></i>
                            <span id="busuanzi_container_site_pv"><span id="busuanzi_value_page_pv"></span></span>    
                        
                        
                            <div class="article-meta-button">
                                <a href='https://gitee.com/liuyi6/liuyi6/raw/hexo/source/_posts/Java运算符.md'> Source </a>
                            </div>
                            <div class="article-meta-button">
                                <a href='https://gitee.com/liuyi6/liuyi6/edit/hexo/source/_posts/Java运算符.md'> Edit </a>
                            </div>
                            <div class="article-meta-button">
                                <a href='https://gitee.com/liuyi6/liuyi6/commits/hexo/source/_posts/Java运算符.md'> History </a>
                            </div>
                        
                    </div>
                
                
    
        <h1 class="article-title" itemprop="name">
            Java运算符
        </h1>
    

            </header>
        
        
        <div class="article-entry" itemprop="articleBody">
        
        
            
        
        
            <p>计算机的最基本用途之一就是执行数学运算， Java也提供了一套丰富的运算符来操纵变量。</p>
<h2 id="算术运算符"><a href="#算术运算符" class="headerlink" title="算术运算符"></a>算术运算符</h2><table>
<thead>
<tr>
<th>操作符</th>
<th>描述 </th>
</tr>
</thead>
<tbody>
<tr>
<td>+</td>
<td>加法 - 相加运算符两侧的值 </td>
</tr>
<tr>
<td>-</td>
<td>减法 - 左操作数减去右操作数 </td>
</tr>
<tr>
<td>*</td>
<td>乘法 - 相乘操作符两侧的值 </td>
</tr>
<tr>
<td>/</td>
<td>除法 - 左操作数除以右操作数 </td>
</tr>
<tr>
<td>％</td>
<td>取余 - 左操作数除以右操作数的余数 </td>
</tr>
<tr>
<td>++</td>
<td>自增: 操作数的值增加1 </td>
</tr>
<tr>
<td>–</td>
<td>自减: 操作数的值减少1 </td>
</tr>
</tbody>
</table>
<p>算数运算符的应用实例如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String []args)&#123;</span><br><span class="line">        System.out.println(10+5);</span><br><span class="line">        System.out.println(10-5);</span><br><span class="line">        System.out.println(10*5);</span><br><span class="line">        System.out.println(10/5);</span><br><span class="line">        System.out.println(10%3);</span><br><span class="line">     &#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">15</span><br><span class="line">5</span><br><span class="line">50</span><br><span class="line">2</span><br><span class="line">1</span><br></pre></td></tr></table></figure></p>
<h2 id="自增自减运算符"><a href="#自增自减运算符" class="headerlink" title="自增自减运算符"></a>自增自减运算符</h2><ol>
<li>自增（++）自减（–）运算符是一种特殊的算术运算符，而自增自减运算符是一个操作数。</li>
<li>前缀自增自减法(++a,–a): 先进行自增或者自减运算，再进行表达式运算</li>
<li>后缀自增自减法(a++,a–): 先进行表达式运算，再进行自增或者自减运算</li>
</ol>
<p>自增自减运算符应用实例如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String []args)&#123;</span><br><span class="line">       int num = 5;</span><br><span class="line">       int a = ++num;</span><br><span class="line">       System.out.println(&quot;前缀++结果为:&quot;+a);</span><br><span class="line">       num = 5;</span><br><span class="line">       int b = num++;</span><br><span class="line">       System.out.println(&quot;后缀++结果为:&quot;+b);</span><br><span class="line">       </span><br><span class="line">       num = 5;</span><br><span class="line">       int c = --num;</span><br><span class="line">       System.out.println(&quot;前缀--结果为:&quot;+c);</span><br><span class="line">       num = 5;</span><br><span class="line">       int d = num--;</span><br><span class="line">       System.out.println(&quot;后缀--结果为:&quot;+d);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">前缀++结果为:6</span><br><span class="line">后缀++结果为:5</span><br><span class="line">前缀--结果为:4</span><br><span class="line">后缀--结果为:5</span><br></pre></td></tr></table></figure></p>
<h2 id="关系运算符"><a href="#关系运算符" class="headerlink" title="关系运算符"></a>关系运算符</h2><table>
<thead>
<tr>
<th>运算符</th>
<th>描述 </th>
</tr>
</thead>
<tbody>
<tr>
<td>==</td>
<td>检查如果两个操作数的值是否相等，如果相等则条件为真。 </td>
</tr>
<tr>
<td>!=</td>
<td>检查如果两个操作数的值是否相等，如果值不相等则条件为真。 </td>
</tr>
<tr>
<td>&gt;</td>
<td>检查左操作数的值是否大于右操作数的值，如果是那么条件为真。 </td>
</tr>
<tr>
<td>&lt;</td>
<td>检查左操作数的值是否小于右操作数的值，如果是那么条件为真。 </td>
</tr>
<tr>
<td>&gt;=</td>
<td>检查左操作数的值是否大于或等于右操作数的值，如果是那么条件为真。 </td>
</tr>
<tr>
<td>&lt;=</td>
<td>检查左操作数的值是否小于或等于右操作数的值，如果是那么条件为真。 </td>
</tr>
</tbody>
</table>
<p>关系运算符的应用如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String []args)&#123;</span><br><span class="line">        System.out.println(5==6);</span><br><span class="line">        System.out.println(5==5);</span><br><span class="line">        System.out.println(&quot;-----&quot;);</span><br><span class="line">        System.out.println(5!=6);</span><br><span class="line">        System.out.println(5!=5);</span><br><span class="line">        System.out.println(&quot;-----&quot;);</span><br><span class="line">        System.out.println(5==6);</span><br><span class="line">        System.out.println(5==5);</span><br><span class="line">        System.out.println(&quot;-----&quot;);</span><br><span class="line">        System.out.println(5&gt;6);</span><br><span class="line">        System.out.println(5&gt;5);</span><br><span class="line">        System.out.println(&quot;-----&quot;);</span><br><span class="line">        System.out.println(5&lt;6);</span><br><span class="line">        System.out.println(5&lt;5);</span><br><span class="line">        System.out.println(&quot;-----&quot;);</span><br><span class="line">        System.out.println(5&gt;=6);</span><br><span class="line">        System.out.println(5&gt;=5);</span><br><span class="line">        System.out.println(&quot;-----&quot;);</span><br><span class="line">        System.out.println(5&lt;=6);</span><br><span class="line">        System.out.println(5&lt;=5);</span><br><span class="line">     &#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">false</span><br><span class="line">true</span><br><span class="line">-----</span><br><span class="line">true</span><br><span class="line">false</span><br><span class="line">-----</span><br><span class="line">false</span><br><span class="line">true</span><br><span class="line">-----</span><br><span class="line">false</span><br><span class="line">false</span><br><span class="line">-----</span><br><span class="line">true</span><br><span class="line">false</span><br><span class="line">-----</span><br><span class="line">false</span><br><span class="line">true</span><br><span class="line">-----</span><br><span class="line">true</span><br><span class="line">true</span><br></pre></td></tr></table></figure></p>
<h2 id="位运算符"><a href="#位运算符" class="headerlink" title="位运算符"></a>位运算符</h2><table>
<thead>
<tr>
<th>操作符</th>
<th>描述</th>
<th>12为1100，5为 0101</th>
</tr>
</thead>
<tbody>
<tr>
<td>C</td>
<td>如果相对应位都是1，则结果为1，否则为0</td>
<td>12&amp;5=4</td>
</tr>
<tr>
<td>&brvbar;</td>
<td>如果相对应位都是0，则结果为0，否则为1</td>
<td>12&brvbar;5=13</td>
</tr>
<tr>
<td>^</td>
<td>异或    如果相对应位值相同，则结果为0，否则为1</td>
<td>12^5=9</td>
</tr>
<tr>
<td>〜</td>
<td>取反    按位取反运算符翻转操作数的每一位，即0变成1，1变成0。</td>
<td>~12=-13</td>
</tr>
<tr>
<td>&lt;&lt;</td>
<td>按位左移运算符。左操作数按位左移右操作数指定的位数。</td>
<td>12&lt;&lt;1=24</td>
</tr>
<tr>
<td>&gt;&gt;</td>
<td>按位右移运算符。左操作数按位右移右操作数指定的位数。</td>
<td>12&gt;&gt;1=6</td>
</tr>
<tr>
<td>&gt;&gt;&gt;</td>
<td>按位右移补零操作符。左操作数的值按右操作数指定的位数右移，移动得到的空位以零填充。</td>
<td>12&gt;&gt;&gt;1=6</td>
</tr>
</tbody>
</table>
<p>参考自：<a href="https://www.cnblogs.com/gavanwanggw/p/7015620.html" target="_blank" rel="noopener">https://www.cnblogs.com/gavanwanggw/p/7015620.html</a></p>
<h2 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h2><p>逻辑运算符的连接的是布尔表达式，要与位运算符做区分。</p>
<table>
<thead>
<tr>
<th>操作符</th>
<th>描述</th>
<th>例子</th>
</tr>
</thead>
<tbody>
<tr>
<td>&amp;&amp;</td>
<td>称为逻辑与运算符。当且仅当两个操作数都为真，条件才为真。</td>
<td>（A &amp;&amp; B）为假。</td>
</tr>
<tr>
<td>&Iota;&Iota;</td>
<td>称为逻辑或操作符。如果任何两个操作数任何一个为真，条件为真。</td>
<td>（A &Iota;&Iota;B）为真。</td>
</tr>
<tr>
<td>！</td>
<td>称为逻辑非运算符。用来反转操作数的逻辑状态。如果条件为true，则逻辑非运算符将得到false。</td>
<td>！（A &amp;&amp; B）为真。</td>
</tr>
</tbody>
</table>
<p>示例代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String []args)&#123;</span><br><span class="line">        System.out.println(true &amp; false);    //false</span><br><span class="line">        System.out.println(true &amp; true);     //true</span><br><span class="line">        System.out.println(false &amp; false);   //false</span><br><span class="line">        System.out.println(false &amp; true);    //false</span><br><span class="line">        </span><br><span class="line">        System.out.println(true | false);    //true</span><br><span class="line">        System.out.println(true | true);     //true</span><br><span class="line">        System.out.println(false | false);   //false</span><br><span class="line">        System.out.println(false | true);    //true</span><br><span class="line">     &#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>短路逻辑运算符</strong><br>‘&amp;&amp;、||’被称为短路逻辑运算符：‘&amp;、|’，单个的逻辑运算符会将左右两个表达式都进行运算得出布尔值，再进行运算。<br>‘短路与’若左边表达式为false则不会对右边的表达式进行判断，因为结果必为false；<br>‘短路或’若左边表达式结果为true则不会对右边的表达式进行判断，因为结果必为true。<br>短语的逻辑运算符相当于普通的逻辑运算符效率更高些。</p>
<p>示例代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String []args)&#123;</span><br><span class="line">        int num = 1;</span><br><span class="line">        System.out.println(false &amp; num++==1);//即使前面为false，右边的表达式还是会继续执行，即num++</span><br><span class="line">        System.out.println(num);//2</span><br><span class="line">        System.out.println(&quot;------------------&quot;);</span><br><span class="line">        num = 1;</span><br><span class="line">        System.out.println(false &amp;&amp; num++==1);//左边的表达式为false，右边的表达式将不会执行，即num不会自增</span><br><span class="line">        System.out.println(num);//1</span><br><span class="line">     &#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">false</span><br><span class="line">2</span><br><span class="line">------------------</span><br><span class="line">false</span><br><span class="line">1</span><br></pre></td></tr></table></figure>
<p>参考自：<a href="https://www.cnblogs.com/yxiaooutlook/p/7606277.html" target="_blank" rel="noopener">https://www.cnblogs.com/yxiaooutlook/p/7606277.html</a></p>
<h2 id="赋值运算符"><a href="#赋值运算符" class="headerlink" title="赋值运算符"></a>赋值运算符</h2><table>
<thead>
<tr>
<th>操作符</th>
<th>描述</th>
<th>例子</th>
</tr>
</thead>
<tbody>
<tr>
<td>=</td>
<td>简单的赋值运算符，将右操作数的值赋给左侧操作数</td>
<td>C = A + B将把A + B得到的值赋给C</td>
</tr>
<tr>
<td>+ =</td>
<td>加和赋值操作符，它把左操作数和右操作数相加赋值给左操作数</td>
<td>C + = A等价于C = C + A</td>
</tr>
<tr>
<td>- =</td>
<td>减和赋值操作符，它把左操作数和右操作数相减赋值给左操作数</td>
<td>C - = A等价于C = C - A</td>
</tr>
<tr>
<td>* =</td>
<td>乘和赋值操作符，它把左操作数和右操作数相乘赋值给左操作数</td>
<td>C <em> = A等价于C = C </em> A</td>
</tr>
<tr>
<td>/ =</td>
<td>除和赋值操作符，它把左操作数和右操作数相除赋值给左操作数</td>
<td>C / = A等价于C = C / A</td>
</tr>
<tr>
<td>（％）=</td>
<td>取模和赋值操作符，它把左操作数和右操作数取模后赋值给左操作数</td>
<td>C％= A等价于C = C％A</td>
</tr>
<tr>
<td>&lt;&lt; =</td>
<td>左移位赋值运算符</td>
<td>C &lt;&lt; = 2等价于C = C &lt;&lt; 2</td>
</tr>
<tr>
<td>&gt;&gt; =</td>
<td>右移位赋值运算符</td>
<td>C &gt;&gt; = 2等价于C = C &gt;&gt; 2</td>
</tr>
<tr>
<td>＆=</td>
<td>按位与赋值运算符</td>
<td>C＆= 2等价于C = C＆2</td>
</tr>
<tr>
<td>^ =</td>
<td>按位异或赋值操作符</td>
<td>C ^ = 2等价于C = C ^ 2</td>
</tr>
</tbody>
</table>
<h2 id="其他运算符"><a href="#其他运算符" class="headerlink" title="其他运算符"></a>其他运算符</h2><p><strong>条件运算符</strong><br>条件运算符也被称为三元运算符。该运算符有3个操作数，并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">variable x = (expression) ? value if true : value if false</span><br></pre></td></tr></table></figure></p>
<p>示例代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String[] args)&#123; </span><br><span class="line">	int a , b; a = 10; // 如果 a 等于 1 成立，则设置 b 为 20，否则为 30 </span><br><span class="line">	b = (a == 1) ? 20 : 30; </span><br><span class="line">	System.out.println( &quot;b = : &quot; + b ); </span><br><span class="line">	// 如果 a 等于 10 成立，则设置 b 为 20，否则为 30 </span><br><span class="line">	b = (a == 10) ? 20 : 30; </span><br><span class="line">	System.out.println( &quot; b = &quot; + b ); </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">b = 30</span><br><span class="line">b = 20</span><br></pre></td></tr></table></figure></p>
<p><strong>instanceof 运算符</strong></p>
<p>该运算符用于操作对象实例，检查该对象是否是一个特定类型（类类型或接口类型）。<br>instanceof运算符使用格式如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">( Object reference variable ) instanceof  (class/interface type)</span><br></pre></td></tr></table></figure></p>
<p>如果运算符左侧变量所指的对象，是操作符右侧类或接口(class/interface)的一个对象，那么结果为真。<br>示例代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">String name = &quot;James&quot;;</span><br><span class="line">boolean result = name instanceof String; // 由于 name 是 String 类型，所以返回真</span><br><span class="line">如果被比较的对象兼容于右侧类型,该运算符仍然返回true。</span><br><span class="line">Class Test&#123;</span><br><span class="line">	public static void main(String[] args)&#123; </span><br><span class="line">		Test a = new Test(); </span><br><span class="line">		boolean result = a instanceof Test; </span><br><span class="line">		System.out.println( result); </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">true</span><br></pre></td></tr></table></figure></p>
<h2 id="运算符优先级"><a href="#运算符优先级" class="headerlink" title="运算符优先级"></a>运算符优先级</h2><table>
<thead>
<tr>
<th>类别</th>
<th>操作符</th>
<th>关联性</th>
</tr>
</thead>
<tbody>
<tr>
<td>后缀</td>
<td>() [] . (点操作符)</td>
<td>左到右</td>
</tr>
<tr>
<td>一元</td>
<td>‘+’  - ！〜</td>
<td>从右到左</td>
</tr>
<tr>
<td>乘性</td>
<td>‘*’ / ％</td>
<td>左到右</td>
</tr>
<tr>
<td>加性</td>
<td>‘+’  -</td>
<td>左到右</td>
</tr>
<tr>
<td>移位</td>
<td>&gt;&gt;  &gt;&gt;&gt;  &lt;&lt;</td>
<td>左到右</td>
</tr>
<tr>
<td>关系</td>
<td>&gt;&gt;   = &lt;&lt;  =</td>
<td>左到右</td>
</tr>
<tr>
<td>相等</td>
<td>==    !=</td>
<td>左到右</td>
</tr>
<tr>
<td>按位与</td>
<td>&amp;</td>
<td>左到右</td>
</tr>
<tr>
<td>按位异或</td>
<td>^</td>
<td>左到右</td>
</tr>
<tr>
<td>按位或</td>
<td>&Iota;</td>
<td>左到右</td>
</tr>
<tr>
<td>逻辑与</td>
<td>&amp;&amp;</td>
<td>左到右</td>
</tr>
<tr>
<td>逻辑或</td>
<td>&Iota;&Iota;</td>
<td>左到右</td>
</tr>
<tr>
<td>条件</td>
<td>？：</td>
<td>从右到左</td>
</tr>
<tr>
<td>赋值</td>
<td>=  +=   -=  *=   /=   ％=  &gt;  &gt;=  &lt;  &lt;=  ＆=   ^= &Iota;=</td>
<td>从右到左</td>
</tr>
</tbody>
</table>
<p>本文参考了：<br><a href="https://www.cnblogs.com/yxiaooutlook/p/7606277.html" target="_blank" rel="noopener">https://www.cnblogs.com/yxiaooutlook/p/7606277.html</a><br><a href="https://www.cnblogs.com/gavanwanggw/p/7015620.html" target="_blank" rel="noopener">https://www.cnblogs.com/gavanwanggw/p/7015620.html</a><br><a href="http://www.runoob.com/java/java-operators.html" target="_blank" rel="noopener">http://www.runoob.com/java/java-operators.html</a><br><a href="https://www.sojson.com/htmlmark.html" target="_blank" rel="noopener">https://www.sojson.com/htmlmark.html</a></p>

            </div>
        
        <footer class="article-footer">
        </footer>
    </div>
</article>


    
<nav id="article-nav">
    
        <a href="/2018/08/12/Java修饰符/" id="article-nav-newer" class="article-nav-link-wrap">
            <strong class="article-nav-caption">上一篇</strong>
            <div class="article-nav-title">
                
                    Java修饰符
                
            </div>
        </a>
    
    
        <a href="/2018/08/12/Java数据类型/" id="article-nav-older" class="article-nav-link-wrap">
            <strong class="article-nav-caption">下一篇</strong>
            <div class="article-nav-title">Java数据类型</div>
        </a>
    
</nav>





    
    




<!-- baidu url auto push script -->
<script type="text/javascript">
    !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=window.location.href,o=document.referrer;if(!e.test(r)){var n="//api.share.baidu.com/s.gif";o?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var t=new Image;t.src=n}}(window);
</script>     
</section>
        </div>
        <footer id="footer">
    <div class="outer">
        <div id="footer-info" class="inner">
            luis &copy; 2018 
            <a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-nd/4.0/80x15.png" /></a>
            <br> Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>. Theme - <a href="https://github.com/zthxxx/hexo-theme-Wikitten">wikitten</a>
            
                <br>
                <span id="busuanzi_container_site_pv"><i class="fa fa-eye"></i> <span id="busuanzi_value_site_pv"></span></span>
                &nbsp;|&nbsp;
                <span id="busuanzi_container_site_pv"><i class="fa fa-user"></i> <span id="busuanzi_value_site_uv"></span></span>
            
        </div>
    </div>
</footer>

        

    
        <script src="/libs/lightgallery/js/lightgallery.min.js"></script>
        <script src="/libs/lightgallery/js/lg-thumbnail.min.js"></script>
        <script src="/libs/lightgallery/js/lg-pager.min.js"></script>
        <script src="/libs/lightgallery/js/lg-autoplay.min.js"></script>
        <script src="/libs/lightgallery/js/lg-fullscreen.min.js"></script>
        <script src="/libs/lightgallery/js/lg-zoom.min.js"></script>
        <script src="/libs/lightgallery/js/lg-hash.min.js"></script>
        <script src="/libs/lightgallery/js/lg-share.min.js"></script>
        <script src="/libs/lightgallery/js/lg-video.min.js"></script>
    
    
        <script src="/libs/justified-gallery/jquery.justifiedGallery.min.js"></script>
    
    
        <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true,
            TeX: {
                equationNumbers: {
                  autoNumber: 'AMS'
                }
            }
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script async src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    



<!-- Custom Scripts -->
<script src="/js/main.js"></script>

    </div>
</body>
</html>